<?php

namespace App;

use App\Model\Article;
use App\Model\Attachment;
use App\Model\Lesson;
use App\Model\News;
use App\Model\Topic;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable{
       notify as protected systemNotify;
    }
    use HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = [
//        'name', 'email', 'password','phone'
//    ];
    protected $guarded = ['password_confirmation'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function attachment()
    {
        return $this->hasMany(Attachment::class);

    }
    public function article()
    {
        return $this->hasMany(Article::class);

    }
    public function topic()
    {
        return $this->hasMany(Topic::class);

    }

    public function getIconAttribute($key)
    {
        return $key ?: asset('images/wechat.jpg');
    }

    //获得当前用户的粉丝
    public function fans()
    {
        return $this->belongsToMany(User::class, 'follows', 'user_id', 'fans_id');
    }

    //该用户关注了谁
    public function follwed()
    {
        return $this->belongsToMany(User::class, 'follows', 'fans_id', 'user_id');
    }

    //取消或者关注
    public function attention(User $user)
    {
        //dd($user->toArray());
        return $this->follwed->contains($user);
    }
    public function notify($instance)
    {
        if(auth()->id()!=$this->id){
            $this->systemNotify($instance);
        }
    }
    public function lesson(){
        return $this->hasMany (Lesson::class);
    }
    public function news(){
        return $this->hasMany(News::class);
    }
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }
}
